/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.sdt.refinement.generator;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import java.util.Vector;
import edu.yonsei.iwb.sdt.datastructure.DecomposeCandidate;
import edu.yonsei.iwb.sdt.datastructure.DecomposeCandidateSet;
import edu.yonsei.iwb.sdt.ontology.PropertyArc;
import edu.yonsei.iwb.sdt.refinement.DLConstructors;
import edu.yonsei.iwb.sdt.refinement.QualifiedConjunctionRefinement;
import edu.yonsei.iwb.sdt.refinement.Refinement;
import edu.yonsei.iwb.sdt.refinement.RefinementType;
import edu.yonsei.iwb.sdt.util.UtilFunctions;

/**
 *
 * @author JDK
 */
public class QualifiedConjunctionRefinementMaker {

    private Model model;
    private Vector<Resource> instanceSet;
    private QuantificationRefinementMaker qrm;

    public QualifiedConjunctionRefinementMaker(Model model, Vector<Resource> instanceSet) {
        this.model = model;
        this.instanceSet = instanceSet;
        this.qrm = new QuantificationRefinementMaker(RefinementType.CONJUNCTION);
    }
    
    public DecomposeCandidateSet refinementGenerator(Refinement bref, DecomposeCandidateSet expandeddcs){
                                
        DecomposeCandidateSet newdcs = new DecomposeCandidateSet();                
//        for (int i = 0; i < expandeddcs.size(); i++) {
//            DecomposeCandidate newdc = expandeddcs.get(i);
//            Vector<Refinement> refset = newdc.getRefinementSet();            
//            for (int j = 0; j < refset.size(); j++) {                
//                Refinement r = refset.get(j);
//                PropertyArc pa = newdc.getPropertyArc();
//                Property p = pa.getProperty();
//                if(r.getBranchingType()){
//                    Vector<Refinement> newrefSet = new Vector<Refinement>();
//                    Vector<Resource> css = this.getSatisfiedInstanceSet(instanceSet, r.getInstanceSet());
//                    Vector<Resource> cloneSet = UtilFunctions.copyInstanceSet(instanceSet);
////                    ExpandingHistory eh = r.getExpandingHistory();
//                    String newcorename = r.getCoreName();
//                    if(newdc.getRefinementType() == RefinementType.QUALIFICATION){
//                        String quan = r.getExpandingHistory().getQuantifierSet().firstElement();
//                        String prop = r.getExpandingHistory().getPropertySet().firstElement().getProperty().getLocalName();                        
//                        newcorename = quan + prop +  ".("+newcorename+")";
//                    }
//                                        
//                    Refinement pref = new QualifiedConjunctionRefinement("",bref.getExpandingHistory(),  bref.getCoreName(), newcorename);
//                    Refinement nref = new QualifiedConjunctionRefinement(DLConstructors.NEGATION, bref.getExpandingHistory(), bref.getCoreName(), newcorename);
//                    
//                    for (int k = 0; k < css.size(); k++) {
//                        Resource result = css.get(k);
//                        if (this.instanceSet.contains(result)) {
//                            pref.setInstanceSet(result);
//                            cloneSet.remove(result);
//                        }
//                    }
//        //            UtilFunctions.printVectorOfResource(cloneSet);
//                    if (!cloneSet.isEmpty() && !pref.getInstanceSet().isEmpty()) {
//                        nref.setInstanceSet(cloneSet);
//                        newrefSet.add(pref);
//                        newrefSet.add(nref);
//                    } else {
//                        continue;
//                    }
//
//                    DecomposeCandidate dc = new DecomposeCandidate(newrefSet, pa, RefinementType.QUALIFIED_CONJUNTION);
//        //            dc.printDecomposeCandidateDetail();
//                    newdcs.add(dc);
//                    
//                }
//                
//            }            
//        }
        
        

        return newdcs;        
    }
    
//    private String rewriteCoreName
  

    private Vector<Resource> getSatisfiedInstanceSet(Vector<Resource> instanceSet, Vector<Resource> expInstanceSet) {
        Vector<Resource> css = new Vector<Resource>();
        
        
        for (int i = 0; i < instanceSet.size(); i++) {            
            if(expInstanceSet.contains(instanceSet.get(i))){
                if(!css.contains(instanceSet.get(i))){
                    css.add(instanceSet.get(i));                    
                }                
            }
        }        

        return css;
    }

    private Vector<Property> copyPropertySet(Vector<Property> propertySet) {
        Vector<Property> copy = new Vector<Property>();

        for (int i = 0; i < propertySet.size(); i++) {
            copy.add(propertySet.get(i));
        }
        return copy;
    }
}
